﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using FangDuShi_Admin.DBUtility;
using System.Data;

namespace FangDuShi_Admin.DAL
{
    /// <summary>
    /// 经纪人 数据类
    /// </summary>
    public class Realtor
    {
        public Realtor()
        {
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsMobile(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Realtor");
            strSql.Append(" where FMobile=@FMobile");
            SqlParameter[] parameters = {
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40)
			};
            parameters[0].Value = mobile;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取真实姓名
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public string GetRealName(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FRealName FROM t_Realtor");
            strSql.Append(" where FMobile=@FMobile");
            SqlParameter[] parameters = {
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40)
			};
            parameters[0].Value = mobile;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }
         /// <summary>
        /// 获取经纪人ID
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public decimal GetRealtorID(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FRealtorID FROM t_Realtor");
            strSql.Append(" where FMobile=@FMobile");
            SqlParameter[] parameters = {
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40)
			};
            parameters[0].Value = mobile;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return decimal.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 获取用户密码
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public string GetUserPassword(string mobile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FPassword FROM t_Realtor");
            strSql.Append(" where FMobile=@FMobile");
            SqlParameter[] parameters = {
					new SqlParameter("@FMobile", SqlDbType.NVarChar,40)
			};
            parameters[0].Value = mobile;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FangDuShi_Admin.Model.Realtor GetModel(decimal realtorID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FRealtorID,FMobile,FNickName,FRealName,FSex,FFacePhotoFile,FICCardNo,FICCardFrontFile,FICCardBackFile,FDistrictNo,FIsAloneRealtor,FAgencyBranchID,FCorpCertFile,FIntroduction,FIsAuthPass,FAuthDate,FBindQQ,FBindXinLangWeibo,FBindWeixin,FRegDate,FRegIP,FLastLoginTime,FLastLoginIP,FLastLoginSystem,FLastActiveTime,FLastActiveIP,FLastActiveLog from t_Realtor ");
            strSql.Append(" where FRealtorID=@FRealtorID");
            SqlParameter[] parameters = {
					new SqlParameter("@FRealtorID", SqlDbType.Decimal)
			};
            parameters[0].Value = realtorID;

            FangDuShi_Admin.Model.Realtor model = new FangDuShi_Admin.Model.Realtor();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FangDuShi_Admin.Model.Realtor DataRowToModel(DataRow row)
        {
            FangDuShi_Admin.Model.Realtor model = new FangDuShi_Admin.Model.Realtor();
            if (row != null)
            {
                if (row["FRealtorID"] != null && row["FRealtorID"].ToString() != "")
                {
                    model.FRealtorID = decimal.Parse(row["FRealtorID"].ToString());
                }
                if (row["FMobile"] != null)
                {
                    model.FMobile = row["FMobile"].ToString();
                }
                if (row["FNickName"] != null)
                {
                    model.FNickName = row["FNickName"].ToString();
                }
                if (row.Table.Columns.Contains("FPassword"))
                {
                    if (row["FPassword"] != null)
                    {
                        model.FPassword = row["FPassword"].ToString();
                    }
                }
                
                if (row["FRealName"] != null)
                {
                    model.FRealName = row["FRealName"].ToString();
                }
                if (row["FSex"] != null)
                {
                    model.FSex = row["FSex"].ToString();
                }
                if (row["FFacePhotoFile"] != null)
                {
                    model.FFacePhotoFile = row["FFacePhotoFile"].ToString();
                }
                if (row["FICCardNo"] != null)
                {
                    model.FICCardNo = row["FICCardNo"].ToString();
                }
                if (row["FICCardFrontFile"] != null)
                {
                    model.FICCardFrontFile = row["FICCardFrontFile"].ToString();
                }
                if (row["FICCardBackFile"] != null)
                {
                    model.FICCardBackFile = row["FICCardBackFile"].ToString();
                }
                if (row["FDistrictNo"] != null)
                {
                    model.FDistrictNo = row["FDistrictNo"].ToString();
                }
                if (row["FIsAloneRealtor"] != null)
                {
                    model.FIsAloneRealtor = row["FIsAloneRealtor"].ToString();
                }
                if (row["FAgencyBranchID"] != null && row["FAgencyBranchID"].ToString() != "")
                {
                    model.FAgencyBranchID = decimal.Parse(row["FAgencyBranchID"].ToString());
                }
                if (row["FCorpCertFile"] != null)
                {
                    model.FCorpCertFile = row["FCorpCertFile"].ToString();
                }
                if (row["FIntroduction"] != null)
                {
                    model.FIntroduction = row["FIntroduction"].ToString();
                }
                if (row["FIsAuthPass"] != null)
                {
                    model.FIsAuthPass = row["FIsAuthPass"].ToString();
                }
                if (row["FAuthDate"] != null && row["FAuthDate"].ToString() != "")
                {
                    model.FAuthDate = DateTime.Parse(row["FAuthDate"].ToString());
                }
                if (row["FBindQQ"] != null)
                {
                    model.FBindQQ = row["FBindQQ"].ToString();
                }
                if (row["FBindXinLangWeibo"] != null)
                {
                    model.FBindXinLangWeibo = row["FBindXinLangWeibo"].ToString();
                }
                if (row["FBindWeixin"] != null)
                {
                    model.FBindWeixin = row["FBindWeixin"].ToString();
                }
                if (row["FRegDate"] != null && row["FRegDate"].ToString() != "")
                {
                    model.FRegDate = DateTime.Parse(row["FRegDate"].ToString());
                }
                if (row["FRegIP"] != null)
                {
                    model.FRegIP = row["FRegIP"].ToString();
                }
                if (row["FLastLoginTime"] != null && row["FLastLoginTime"].ToString() != "")
                {
                    model.FLastLoginTime = DateTime.Parse(row["FLastLoginTime"].ToString());
                }
                if (row["FLastLoginIP"] != null)
                {
                    model.FLastLoginIP = row["FLastLoginIP"].ToString();
                }
                if (row["FLastLoginSystem"] != null)
                {
                    model.FLastLoginSystem = row["FLastLoginSystem"].ToString();
                }
                if (row["FLastActiveTime"] != null && row["FLastActiveTime"].ToString() != "")
                {
                    model.FLastActiveTime = DateTime.Parse(row["FLastActiveTime"].ToString());
                }
                if (row["FLastActiveIP"] != null)
                {
                    model.FLastActiveIP = row["FLastActiveIP"].ToString();
                }
                if (row["FLastActiveLog"] != null)
                {
                    model.FLastActiveLog = row["FLastActiveLog"].ToString();
                }
            }
            return model;
        }
    }
}
